package com.ruoyi.system.controller;

import com.ruoyi.common.utils.MinioUtil;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MineTraining;
import com.ruoyi.system.service.IMineTrainingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 培训Controller
 * 
 * @author ruoyi
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/system/training")
public class MineTrainingController extends BaseController
{
    @Autowired
    private IMineTrainingService mineTrainingService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 查询培训列表
     */
    @PreAuthorize("@ss.hasPermi('system:training:list')")
    @GetMapping("/list")
    public TableDataInfo list(MineTraining mineTraining)
    {
        startPage();
        List<MineTraining> list = mineTrainingService.selectMineTrainingList(mineTraining);
        return getDataTable(list);
    }

    /**
     * 导出培训列表
     */
    @PreAuthorize("@ss.hasPermi('system:training:export')")
    @Log(title = "培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MineTraining mineTraining)
    {
        List<MineTraining> list = mineTrainingService.selectMineTrainingList(mineTraining);
        ExcelUtil<MineTraining> util = new ExcelUtil<MineTraining>(MineTraining.class);
        util.exportExcel(response, list, "培训数据");
    }

    /**
     * 获取培训详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:training:query')")
    @GetMapping(value = "/{trainingId}")
    public AjaxResult getInfo(@PathVariable("trainingId") Long trainingId)
    {
        return success(mineTrainingService.selectMineTrainingByTrainingId(trainingId));
    }

    /**
     * 根据 examId 查询培训信息
     */
    @PreAuthorize("@ss.hasPermi('system:training:query')")
    @GetMapping(value = "/exam/{examId}")
    public AjaxResult getInfoByExamId(@PathVariable("examId") Long examId)
    {
        return success(mineTrainingService.selectMineTrainingByExamId(examId));
    }

    /**
     * 新增培训
     */
    @PreAuthorize("@ss.hasPermi('system:training:add')")
    @Log(title = "培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MineTraining mineTraining)
    {
        return success(mineTrainingService.insertMineTraining(mineTraining));
    }

    /**
     * 修改培训
     */
    @PreAuthorize("@ss.hasPermi('system:training:edit')")
    @Log(title = "培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MineTraining mineTraining)
    {
        return toAjax(mineTrainingService.updateMineTraining(mineTraining));
    }

    /**
     * 删除培训
     */
    @PreAuthorize("@ss.hasPermi('system:training:remove')")
    @Log(title = "培训", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainingIds}")
    public AjaxResult remove(@PathVariable Long[] trainingIds)
    {
        return toAjax(mineTrainingService.deleteMineTrainingByTrainingIds(trainingIds));
    }

    /**
     * 上传培训封面图片
     */
    @PreAuthorize("@ss.hasPermi('system:exam:edit')")
    @Log(title = "培训封面上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadImage/{trainingId}")
    public AjaxResult uploadTrainingImage(
            @PathVariable Long trainingId,
            @RequestParam("file") MultipartFile file) throws Exception {

        // 上传图片到MinIO
        String imageUrl = minioUtil.uploadTrainingImage(file, trainingId);

        // 更新数据库
        MineTraining training = mineTrainingService.selectMineTrainingByTrainingId(trainingId);
        training.setCoverUrl(imageUrl);
        mineTrainingService.updateMineTraining(training);

        return AjaxResult.success("图片上传成功", imageUrl);
    }
}
